let lotteryDrawConfig = function(){
    let html = '<div style="margin-top: 10px;margin-right: 20px">\n' +
        '</div>' +
        '    <input id="addlotteryDrawConfigHidden" value="" hidden="hidden">\n' +
        '    <input id="editlotteryDrawConfigHidden" value="" hidden="hidden">\n' +
        '<div style="margin-left: 10px;margin-right: 10px"><table class="layui-hide" id="test" lay-filter="test"></table></div>';

    layui.use(['table', 'form', 'jquery', 'element'], function () {
        var table = layui.table
            , form = layui.form
            , $ = layui.jquery
            , element = layui.element;

        $("#mainbody").html(html);
        //表格加载
        table.render({
            elem: '#test'
            , url: 'lotteryDrawConfig/get'
            , toolbar: 'default'
            , title: '明细表'
            , id: 'layuiTable' //用作主键 做刷新用
            , page: true
            , limit: 10//默认每页显示数量
            , limits: [10, 20, 30]//每页显示数量类型
            , cols: [[
                {type: 'checkbox'}
                , {field: 'award', title: '奖品', width: 80, align: 'center'}
                , {field: 'odds', title: '中奖概率%', width: 150, align: 'center'}
                , {field: 'createTime', title: '创建时间', width: 180, align: 'center', templet: function (res) {
                        //JS时间戳转时间字符串
                        let d = new Date(res.createTime);
                        let date = (d.getFullYear()) + "-" +
                            (d.getMonth() + 1) + "-" +
                            (d.getDate()) + " " +
                            (d.getHours()) + ":" +
                            (d.getMinutes()) + ":" +
                            (d.getSeconds());
                        let str = date.toString();
                        let monthIndex = str.indexOf("-");
                        let monthStr = str.substring(monthIndex+1,str.length);
                        let dateIndex = monthStr.indexOf("-");
                        monthStr = monthStr.substring(0,dateIndex);
                        if(monthStr.length==1){
                            let str1 = str.substring(0,monthIndex+1);
                            let str2 = str.substring(monthIndex+1,str.length);
                            str1 = str1+"0";
                            str = str1+str2;
                        }
                        dateIndex = monthIndex+3;
                        let dateStr = str.substring(dateIndex+1,str.length);
                        let blankIndex = dateStr.indexOf(" ");
                        dateStr = dateStr.substring(0,blankIndex);
                        if(dateStr.length==1){
                            let str1 = str.substring(0,dateIndex+1);
                            let str2 = str.substring(dateIndex+1,str.length);
                            str1 = str1+"0";
                            str = str1+str2;
                        }

                        return str;
                    }
                }

            ]]
        });


        form.render();


        //头工具栏事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id)
                , data = checkStatus.data; //获取选中的数据
            switch (obj.event) {
                case 'add':
                    layer.open({
                        title: ['添加', 'font-size:18px;']
                        , shade: 0.6 //遮罩透明度
                        , area: ['600px','300px']//宽度
                        , type: 2
                        , anim: 1 //0-6的动画形式，-1不开启
                        , content: "jump?url=game/addLotteryDrawConfig"
                        , success: function (layero, index) {
                            //layer高度自适应
                            // layer.iframeAuto(index);
                        }
                        , end: function () {
                            let handle_status = $("#addlotteryDrawConfigHidden").val();

                            if (handle_status == "success") {

                                $("#addlotteryDrawConfigHidden").val("");
                                layer.msg('添加成功', {
                                    icon: 1,
                                    time: 1000 //2秒关闭（如果不配置，默认是3秒）
                                }, function () {
                                    table.reload('layuiTable');
                                });
                            } else if (handle_status == 2) {
                                layer.msg('添加失败',
                                    {icon: 2, time: 1000}
                                )
                            }
                        }

                    })
                    break;
                case 'update':
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                    } else if (data.length > 1) {
                        layer.msg('只能同时编辑一个');
                    } else {
                        $("#editlotteryDrawConfigHidden").val(JSON.stringify(data[0]));
                        layer.open({
                            title: ['编辑', 'font-size:18px;']
                            , shade: 0.6 //遮罩透明度
                            , area: ['600px','300px']//宽度
                            , type: 2
                            , anim: 1 //0-6的动画形式，-1不开启
                            , content: "jump?url=game/editLotteryDrawConfig"
                            , end: function () {
                                let handle_status = $("#editlotteryDrawConfigHidden").val();

                                if (handle_status == "success") {

                                    $("#editlotteryDrawConfigHidden").val("");
                                    layer.msg('添加成功', {
                                        icon: 1,
                                        time: 1000 //2秒关闭（如果不配置，默认是3秒）
                                    }, function () {
                                        table.reload('layuiTable');
                                    });
                                } else if (handle_status == "error") {
                                    layer.msg('添加失败',
                                        {icon: 2, time: 1000}
                                    )
                                }
                            }

                        })
                        break;
                    }
                    break;
                case 'delete':
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                    } else {
                        //页面删除效果
                        let arr = new Array();
                        data.forEach(value=>{
                            arr.push(value.id);
                        })
                        layer.confirm("确定删除吗?",{

                                btn: ['确定', '取消']
                                ,title:"提示"
                            }, function (index, layero) {
                                console.log(arr);
                                $.ajax({
                                    type: "POST",
                                    url: "lotteryDrawConfig/delete",
                                    traditional: true,
                                    dataType: "json",
                                    contentType: "application/json",
                                    data: JSON.stringify(arr),
                                    success: function (data) {
                                        console.log(data);
                                        layer.msg("删除成功!",{ icon: 1, time: 1000 });
                                        table.reload('layuiTable');
                                    },
                                    error: function () {

                                        layer.msg(JSON.parse(data.responseText).message,{ icon: 1, time: 1000 });
                                    }
                                });
                            }
                        );

                    }
                    break;
            }
            ;
        });



    })
}
